<script setup>
import { ref, onMounted, computed } from 'vue';
import request from "@/utils/request.js";
import { ElMessage } from 'element-plus';
import { useUserStore } from '@/stores/user';

const userStore = useUserStore();
const riderInfo = ref({});
const loading = ref(false);
const statusOptions = [
  { value: 'AVAILABLE', label: '在线' },
  { value: 'BUSY', label: '忙碌' },
  { value: 'OFFLINE', label: '离线' }
];
const stats = ref({
  pendingCount: 0,
  assignedCount: 0,
  pickedCount: 0,
  deliveredCount: 0,
  totalDeliveries: 0,
  rating: 0
});

// 从Pinia获取用户信息
const userInfo = computed(() => userStore.user || {});

// 获取骑手信息
const getRiderInfo = async () => {
  loading.value = true;
  try {
    if (userStore.userId) {
      const { data: res } = await request.get(`/rider/user/${userStore.userId}`);
      if (res) {
        riderInfo.value = res;
        getStats(res.id);
      }
    }
  } catch (error) {
    console.error('获取骑手信息失败', error);
    ElMessage.error('获取骑手信息失败');
  } finally {
    loading.value = false;
  }
  // console.log(riderInfo.value);
  
};

// 获取骑手统计信息
const getStats = async (riderId) => {
  try {
    const { data: res } = await request.get('/rider/stats', {
      params: { riderId }
    });
    if (res) {
      stats.value = res;
    }
  } catch (error) {
    console.error('获取统计信息失败', error);
    ElMessage.error('获取统计信息失败');
  }
};

// 更新骑手状态
const updateStatus = async (status) => {
  try {
    await request.put(`/rider/${riderInfo.value.id}/status`, { status });
    ElMessage.success('状态更新成功');
    getRiderInfo(); // 刷新数据
  } catch (error) {
    console.error('更新状态失败', error);
    ElMessage.error('更新状态失败');
  }
};

onMounted(() => {
  getRiderInfo();
});
</script>

<template>
  <div class="rider-info">
    <el-card class="info-card" v-loading="loading">
      <template #header>
        <div class="card-header">
          <span class="title">骑手信息</span>
        </div>
      </template>
      
      <el-descriptions :column="2" border>
        <el-descriptions-item label="骑手ID">{{ riderInfo.id }}</el-descriptions-item>
        <el-descriptions-item label="用户名">{{ userStore.username }}</el-descriptions-item>
        <el-descriptions-item label="评分">
          <el-rate v-model="stats.rating" disabled show-score text-color="#ff9900" />
        </el-descriptions-item>
        <el-descriptions-item label="当前状态">
          <el-tag :type="riderInfo.status === 'AVAILABLE' ? 'success' : riderInfo.status === 'BUSY' ? 'warning' : 'info'">
            {{ riderInfo.status === 'AVAILABLE' ? '在线' : riderInfo.status === 'BUSY' ? '忙碌' : '离线' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="注册时间" :span="2">
          {{ new Date(riderInfo.createTime).toLocaleString() }}
        </el-descriptions-item>
      </el-descriptions>

      <div class="status-actions">
        <span class="action-label">修改状态:</span>
        <el-radio-group v-model="riderInfo.status" @change="updateStatus">
          <el-radio-button 
            v-for="option in statusOptions" 
            :key="option.value" 
            :value="option.value"
          >
            {{ option.label }}
          </el-radio-button>
        </el-radio-group>
      </div>
    </el-card>

    <el-card class="stats-card">
      <template #header>
        <div class="card-header">
          <span class="title">配送统计</span>
        </div>
      </template>
      
      <el-row :gutter="20">
        <el-col :span="6">
          <div class="stat-item">
            <div class="stat-value">{{ stats.totalDeliveries || 0 }}</div>
            <div class="stat-label">总配送单数</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-item">
            <div class="stat-value">{{ stats.assignedCount || 0 }}</div>
            <div class="stat-label">待取件</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-item">
            <div class="stat-value">{{ stats.pickedCount || 0 }}</div>
            <div class="stat-label">配送中</div>
          </div>
        </el-col>
        <el-col :span="6">
          <div class="stat-item">
            <div class="stat-value">{{ stats.deliveredCount || 0 }}</div>
            <div class="stat-label">已送达</div>
          </div>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<style scoped>
.rider-info {
  padding: 20px;
}

.info-card, .stats-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.status-actions {
  margin-top: 20px;
  display: flex;
  align-items: center;
}

.action-label {
  margin-right: 10px;
  font-weight: bold;
}

.stat-item {
  text-align: center;
  padding: 20px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: #409EFF;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 14px;
  color: #606266;
}
</style>